<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $title
 * @property string $name
 * @property string $email
 * @property string $phone
 * @property string $address
 * @property string $date_birth
 * @property integer $is_active
 * @property integer $ordering
 */
class User extends CActiveRecord
{
    var $re_password;
    var $re_email;
    public $verifyCode;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('username, password ', 'required', 'on'=>'create register'),
			array('name, email,re_password, re_email', 'required','on'=>'register'),
            array('is_active, ordering', 'numerical', 'integerOnly'=>true),
			array('username, title', 'length', 'max'=>45),
			array('password', 'length', 'max'=>254),
            array('re_password', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match",'on'=>'register'),
            array('name', 'length', 'max'=>200),
            array('email', 'email','message'=>"Địa chỉ email này không chính xác."),
            array('email', 'unique','message'=>'Địa chỉ email này đã tồn tại!'),
            array('username', 'unique','message'=>'Username này đã tồn tại!'),
            array('re_email', 'compare', 'compareAttribute'=>'email', 'message'=>"Emails don't match"),
            array('phone', 'length', 'max'=>50),
            array('address', 'length', 'max'=>500),
            array('date_birth', 'safe'),
            array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements() ,'on'=>'register'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
            array('id, username, password, title, name, email, phone, address, date_birth, is_active, ordering', 'safe', 'on'=>'search'),

            array('username, password', 'safe', 'on'=>'create'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Tên Đăng Nhập',
			'password' => 'Mật Khẩu',
			'title' => 'Tiêu Đề',
            'name' => 'Họ Tên',
            'email' => 'Email',
            'phone' => 'Số Điện Thoại',
            'address' => 'Địa Chỉ',
            'date_birth' => 'Ngày Sinh',
            'is_active' => 'Hoạt Động',
            'ordering' => 'Số Thứ Tự',
            're_password'=> 'Nhập Lại Mật Khẩu',
            're_email'=> 'Nhập Lại Email',
            'verifyCode'=>'Mã Xác Nhận'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('title',$this->title,true);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('phone',$this->phone,true);
        $criteria->compare('address',$this->address,true);
        $criteria->compare('date_birth',$this->date_birth,true);
        $criteria->compare('is_active',$this->is_active);
        $criteria->compare('ordering',$this->ordering);

        return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    public function beforeSave() {
        $this->password=md5($this->password);
        //Yii::app()->getSecurityManager()->setEncryptionKey(Yii::app()->params['myKey']);
        //$this->password = utf8_encode(Yii::app()->getSecurityManager()->encrypt($this->password));
        return true;
    }
    public function afterFind()
    {
        //Yii::app()->getSecurityManager()->setEncryptionKey(Yii::app()->params['myKey']);
        //$this->password = Yii::app()->getSecurityManager()->decrypt(utf8_decode($this->password));
    }
    public function afterSave(){

    }
}
